﻿#define _DebugDrawBorder_uomBHP_ExpandClientRectange

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using DienLib.SummitLib;
using System.Diagnostics;

namespace DienLib.SummitLib
{
    /// <summary>
    /// Wrapper for Add Perforation Form (Tabpage 3 Summit)
    /// </summary>
    public class PerfLib : IDisposable
    {
        #region Remote VARIABLE
        const string PERFPAGE_BUT_CANCEL = "_btnCancel";
        ButtonD a_btnCancel;
        const string PERFPAGE_BUT_OK = "_btnOK";
        ButtonD a_btnOK;
        const string PERFPAGE_GRID_PERF = "_grdPerforation";
        DataGridD a_grdPerforation;
        #endregion

        private Form _form;
        public PerfLib(Form PerfWindow)
        {
            if (PerfWindow == null) throw new NullReferenceException();
            if (PerfWindow.IsDisposed) throw new InvalidOperationException("AddPerforation already Disposed or Closed !");
            this._form = PerfWindow;
            #region Init Remote Variable
            a_btnCancel = new ButtonD(DienLib.Reflector.GetField<Button>(_form, PERFPAGE_BUT_CANCEL));
            a_btnOK = new ButtonD(DienLib.Reflector.GetField<Button>(_form, PERFPAGE_BUT_OK));
            a_grdPerforation = new DataGridD(DienLib.Reflector.GetField<DataGrid>(_form, PERFPAGE_GRID_PERF));
            #endregion
        }

        public static PerfLib FindPerfLib()
        {
            // Get Summit Main Form
            var list = DienLib.WinAPI.FindWindowLike(Process.GetCurrentProcess(), "Perforation", FindCulture.EXACT_MATCH);
            if (list.Count == 0) return null;

            IntPtr hWnd = list[0]; // Only 1 Perf Window at a time

            if (hWnd == IntPtr.Zero) { MessageBox.Show("There is no Perforation Window Open ! Please Click Perforation at Page 3 first !"); return null; }

            Form _formPerf;
            try { _formPerf = (Form)Control.FromHandle(hWnd); }
            catch (Exception ex) { MessageBox.Show("Cannot get Form Perforation from Handle ! Something wrong"); throw ex; }
            if (_formPerf == null) { MessageBox.Show("Cannot get Form Perforation from Handle ! Something wrong"); return null; }

            try { return new PerfLib(_formPerf); }
            catch (Exception ex) { MessageBox.Show("Something Wrong when Create new PerfLib object form Active Form !\r\n" + ex.Message, "WARNING : Something wrong !"); return null; }
        }

        public void PerformClickOK()
        {
            a_btnOK.PerformClick(); // Try catch truong hop dong form bao NULL Button
        }

        public void PerformClickCANCEL()
        {
            a_btnCancel.PerformClick(); // Try catch truong hop dong form bao NULL Button
        }

        public void AddPerforation(int gun, int run, double mTopShot, double mBotShot, Enums.GunSize gunSize, Enums.SPF sPF, Enums.Polarity polarity, string comment)
        {
            System.Data.DataSet dsPerf = (System.Data.DataSet)a_grdPerforation.DataSource;
            System.Data.DataTable dtPerf = dsPerf.Tables[0];
            //dtPerf.Rows.Add("OID", "JOB_OID", "LOG_RUN_TYPE_CD", "GUN_NBR", "RUN_NBR", "BTM_MD", "TOP_MD", "TOOL_SZ", "SHOT_DENS", "PLTRY", "CMNT"); // ColumnName or Caption
            dtPerf.Rows.Add(null, null, null, gun.ToString(), run.ToString(), mBotShot.ToString(), mTopShot.ToString(), Enums.eGunSize.ToData(gunSize), Enums.eSPF.ToData(sPF), Enums.ePolarity.ToData(polarity), comment);
            //System.Data.DataRow drPerf = dtPerf.Rows[0];
            //drPerf.ItemArray[10] += "HEHEHE";
            //DienLib.WinAPI.WriteNotePad((string)dtPerf.Rows[0].ItemArray[10]);
        }

        public void AddPerforation(PERFROW perfRow)
        {
            System.Data.DataTable dtPerf = ((System.Data.DataSet)a_grdPerforation.DataSource).Tables[0];
            dtPerf.Rows.Add(null, null, null, perfRow.GetGun(), perfRow.GetRun(), perfRow.GetBottomShot(), perfRow.GetTopShot(), perfRow.GetGunSize(), perfRow.GetShotDensity(), perfRow.GetPolarity(), perfRow.Comment);
        }

        public void AddPerforation(PERFROW[] perfRows, int DelayMS)
        {
            foreach (PERFROW perfRow in perfRows)
            {
                AddPerforation(perfRow);
                if (DelayMS > 0) System.Threading.Thread.Sleep(DelayMS);
            }
        }


        #region DISPOSE METHOD
        // Dispose() calls Dispose(true)
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        // NOTE: Leave out the finalizer altogether if this class doesn't 
        // own unmanaged resources itself, but leave the other methods
        // exactly as they are. 
        ~PerfLib()
        {
            // Finalizer calls Dispose(false)
            Dispose(false);
        }

        // The bulk of the clean-up code is implemented in Dispose(bool)
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // free managed resources, (detach event ?)
                if (this._form != null) this._form = null; // Khong keu this._refobj.Dispose vi Reference toi object do. Ngoai ra do pass reference nen this._refobj = null khong anh huong gi toi object goc
                if (this.a_btnCancel != null) this.a_btnCancel = null;
                if (this.a_btnOK != null) this.a_btnOK = null;
                if (this.a_grdPerforation != null) this.a_grdPerforation = null;
            }

            /* free native resources if there are any.
            if (nativeResource != IntPtr.Zero) 
            {
                Marshal.FreeHGlobal(nativeResource);
                nativeResource = IntPtr.Zero;
            }*/
        }
        #endregion

    }
}

